<?php
class kst_po_auto_model extends MY_Model {
	var $model = 'kst_pembelian';
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT a.no_po,TO_CHAR(a.tgl_order,'DD/MM/YYYY') tgl_order,a.kd_vendor,a.jenis_pembayaran,c.nama_vendor 
                    FROM kst_pembelian a
                           JOIN kst_vendor c ON a.kd_vendor = c.kd_vendor
					WHERE	(a.no_po LIKE '%".post('keyword')."%')
					ORDER BY ".(post('sort')=='' ? 'a.no_po' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
		
	function Add($arr_data){
		$this->db->insert('kst_pembelian', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('kst_pembelian', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_pkmaster){
		$result = $this->db->get_where('kst_pembelian', array('no_po' => $as_pkmaster));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_pkmaster)
	{
		$result	= $this->db->query("SELECT a.no_po,TO_CHAR(a.tgl_order,'DD/MM/YYYY') tgl_order,a.kd_vendor,a.jenis_pembayaran,c.nama_vendor 
                    				FROM kst_pembelian a
                           			JOIN kst_vendor c ON a.kd_vendor = c.kd_vendor
									WHERE	a.no_po = '".$as_pkmaster."'");
		
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			$this->db->delete('kst_pembelian_detail', $arr_rule);
			$this->db->delete('kst_pembelian', $arr_rule);	
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>